//
//  Route.m
//  SZBus
//
//  Created by Kengi Chan on 10/26/09.
//  Copyright 2009 IDMT. All rights reserved.
//

#import "Route.h"
#import "SZBusAppDelegate.h"
#include <sqlite3.h>

static sqlite3 *database = nil;

@implementation Route

@synthesize routeID, routeName, routeType, routeTitle, routeDesc;

+ (void) getInitialDataToDisplay:(NSString *)dbPath {
	SZBusAppDelegate *appDelegate = (SZBusAppDelegate *)[[UIApplication sharedApplication] delegate];
	if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
		const char *sql = "select id, type, title from route";
		sqlite3_stmt *selectstmt;
		int returnValue = sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) ;
		if(returnValue == SQLITE_OK) {
			while(sqlite3_step(selectstmt) == SQLITE_ROW) {
				NSString *primaryKey = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
				Route *routeObj = [[Route alloc] initWithPrimaryKey:primaryKey];
				routeObj.routeName = primaryKey;
				routeObj.routeType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
				routeObj.routeTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
				[appDelegate.routeArray addObject:routeObj];
//				NSLog(@"route %@ %@",routeObj.routeName,routeObj.routeType);
				[routeObj release];
			}
		}else{
			printf("%s\n", sqlite3_errmsg(database));
		}	
	}else{		
		sqlite3_close(database);
	}
}

+ (void) finalizeStatements {
	if(database) sqlite3_close(database);	
}
		
//- (id) initWithPrimaryKey:(NSInteger) pk {			
- (id) initWithPrimaryKey:(NSString *) pk {
	[super init];
	routeID = pk;
	return self;
}

- (void) dealloc {
	[routeID release];
	[routeName release];
	[routeType release];
	[routeTitle release];
	[routeDesc release];
	[super dealloc];
}
@end
